<?php

/**
 * Contrôleur des magasins
 */
class magasin_controller extends Component
{
    // Attributs
    const byPage = 10;
    const page_identifier = 'page-';
    public $_pageTotal;

    /**
     * Constructeur
     */
    public function __construct()
    {
    }

    /**
     * Données à envoyer à la vue par défaut
     */
    public function defaut()
    {
        if (isset($_GET['param'][0])) {
            if (preg_match("/".self::page_identifier."/i", $_GET['param'][0])) {
                $p = explode(self::page_identifier, $_GET['param'][0]);
                $this->getAll($p[1]);
            } else {
                $tmp = explode('-',$_GET['param'][0]);
                $this->getOne($tmp[0]);
            }
        } else {
            $this->getAll();
        }
    }

    /**
     * Sélection d'une ville
     *
     * @param: unsigned $id L'id d'une ville
     */
    private function getOne($id)
    {
        $data = Base::Load(CLASS_SHOP)->get($id);

        $userTMP = Base::Load(CLASS_USER)->getAll();
        foreach($userTMP as $tmp){
            if(isset($tmp['lat']) && isset($tmp['lng'])){
                $users[] = $tmp;
            }
        }
        
        $coms = Base::Load(CLASS_COMMENTAIRE)->findBy('object_id', (string) $id, true);
        $data['ville'] = Base::Load(CLASS_LOC)->getVille($data['ville']);

        if(!empty($data['n_nb1']))
            $data['note1'] = round($data['n_total1'] / $data['n_nb1'], 2);
        else
            $data['note1'] = 0;
        if(!empty($data['n_nb2']))
            $data['note2'] = round($data['n_total2'] / $data['n_nb2'], 2);
        else
            $data['note2'] = 0;
        if(!empty($data['n_nb3']))
            $data['note3'] = round($data['n_total3'] / $data['n_nb3'], 2);
        else
            $data['note3'] = 0;
        
        $this->_view->assign('chasseurs', $users);
        $this->addBreadCrumb($data['name']);
        $this->_view->assign('coms', $coms);
        $this->_view->assign('data', $data);
        $this->_view->assign('user_connected', Base::Load(CLASS_USER)->isConnect());
        $this->_view->addBlock('content', 'magasin.tpl');
    }

    /**
     * Récupération de toutes les magasins
     *
     * @param unsigned $page L'endroit du tableau à visualiser (pagination)
     */
    private function getAll($page = 1)
    {
        $dataTmp = Base::Load(CLASS_SHOP)->getAll();
        $this->_pageTotal = ceil(count($dataTmp) / self::byPage) + 1;
        $start = self::byPage * $page - self::byPage;
        $pageStart = $page - 5;

        if ($pageStart < 1) {
            $pageStart = 1;
        }
        $i = 0;
        foreach ($dataTmp as $d) {
            if ($i >= $start) {
                if ($i < $start + self::byPage) {
                    $data[$i] = $d;
                } else {
                    break;
                }
            }
            $i++;
        }

        foreach($data as $k => $d){
            $data[$k]['ville'] = Base::Load(CLASS_LOC)->getVille($d['ville']);
        }

        $this->_view->assign('pageTotal', $this->_pageTotal);
        $this->_view->assign('page', $page);
        $this->_view->assign('pageStart', $pageStart);
        $this->_view->assign('pageStop', $pageStart + self::byPage - 1);
        $this->_view->assign('data', $data);

        if (isset($_GET['ajax']) && $_GET['ajax']) {
            $this->_view->assign('ajax', true);
            Base::Load(CLASS_BASE)->setLayout('vide.tpl');
            $this->_view->addBlock('data', 'list.tpl');
        } else {
            $this->_view->addBlock('content', 'list.tpl');
        }
    }

    public function getSelect()
    {
    }

    public function POST_Commentaire(array $data)
    {   
        if (!empty($data['message'])) {
            $data['user'] = $_SESSION['user']['uid'];
            Base::Load(CLASS_COMMENTAIRE)->save($data);
            header('Location: ' . HTTP_HOST_REQUEST);
        }
    }
}


/* -- Fin du fichier -- */